import React from "react";
import { staticModel } from "../g6edit/structureData";

export default class Pannel extends React.Component {
  handleChage = (name, value, isEmpty = false) => {
    const obj = {};
    const newValue = value || "empty";
    obj[name] = isEmpty ? value : newValue;
    this.setState({
      ...obj,
    });
  };

  handleGraphChange = (name, value, setState = true) => {
    const { updateGraph } = this.props;
    // if (value) {
    updateGraph(name, value);
    // }
    if (setState) {
      this.handleChage(name, "", true);
    }
  };

  handleChangeMultiProperties = (
    name,
    obj,
    modelProperties,
    shape,
    setState = true,
  ) => {
    const oldModel = staticModel[shape];
    // if (obj) {
    const { properties = {} } = modelProperties || {};
    const newobj = { ...obj };
    const model = {
      ...oldModel,
      ...modelProperties,
      properties: { ...properties, ...newobj },
    };
    const { updateGraph } = this.props;
    updateGraph("model", model);
    // }
    if (setState) {
      this.handleChage(name, "", true);
    }
  };

  handleChangeMultiProperties2 = (
    name,
    arr,
    modelProperties,
    shape,
    setState = true
  ) => {
    const oldModel = staticModel[shape];
    const { properties = {} } = modelProperties || {};
    properties[name] = arr;
    const model = {
      ...oldModel,
      ...modelProperties,
      properties: { ...properties },
    };
    const { updateGraph } = this.props;
    updateGraph("model", model);
    // }
    if (setState) {
      this.handleChage(name, "", true);
    }
  };

  handleChangeProperties = (
    name,
    value,
    modelProperties,
    shape,
    setState = true,
  ) => {
    this.handleChangeMultiProperties(
      name,
      { [name]: value },
      modelProperties,
      shape,
      setState,
    );
  };

  handleChangeProperties2 = (
    name,
    value,
    modelProperties,
    shape,
    setState = true,
  ) => {
    this.handleChangeMultiProperties2(
      name,
      value,
      modelProperties,
      shape,
      setState,
    );
  };

  handlePrePropChange = (name, value, cb) => {
    const { modelProperties, handleModelPropChange } = this.props;
    const newObj = {};
    newObj[name] = value;
    handleModelPropChange({ ...modelProperties, ...newObj }, cb);
  };

  handlePrePropChangeObj = (obj, cb) => {
    const { modelProperties, handleModelPropChange } = this.props;
    handleModelPropChange({ ...modelProperties, ...obj }, cb);
  };
}
